package com.company.control;

import com.company.bean.GoodssalesBean;
import com.company.model.GoodModel;
import com.company.model.UserModel;
import com.company.util.MyUtils;
import com.mysql.cj.protocol.a.MysqlBinaryValueDecoder;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "UpdateGoodsServlet", value = "/UpdateGoodsServlet")
public class UpdateGoodsServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //设置编码值
        request.setCharacterEncoding("utf-8");

        //1.获取表单数据
        GoodssalesBean goodssalesBean = MyUtils.getNewInstance(GoodssalesBean.class);
        goodssalesBean.setDEPTNO(Integer.parseInt(request.getParameter("DEPTNO")));
        goodssalesBean.setGOODSID(request.getParameter("GOODSID"));
        goodssalesBean.setGOODSNAME(request.getParameter("GOODSNAME"));
        goodssalesBean.setGOODSPRICE(Float.parseFloat(request.getParameter("GOODSPRICE")));

        //4.数据保存成功之后，跳转的保存结果显示页面
        if (MyUtils.getNewInstance(GoodModel.class).UpdateGoods(goodssalesBean)){
            //调用Model层进行查询操作，将用户数据查询出来，保存到session中
            List<GoodssalesBean> goodssalesBeans2 = MyUtils.getNewInstance(GoodModel.class)
                   .selectGood();
            //在跳转之前，需要将显示在成功页面的数据进行session缓存
            request.getSession().setAttribute("listGood",goodssalesBeans2);
            //跳转到添加成功页面
            request.getRequestDispatcher("/WEB-INF/views/GoodList5.jsp").forward(request,response);
        }else {
            //跳转到用户信息页面
            request.getRequestDispatcher("/WEB-INF/UpdateGoods.jsp").forward(request,response);
        }
    }

}
